package com.aisino.api.model;

import com.aisino.api.enums.EntityHumitureInfoEnum;
import lombok.Data;
import java.util.List;

@Data
public class HumitureInfoRequest {
    private String appId;
    private String timestamp;
    private String reqId;
    private String uscc;
    private String data;
    private List<HumitureData> itemList;
    private String sign;

    @Data
    public static class HumitureData {
        private String sbazqy;    // 设备安装区域: 1常温 2阴凉 3冷藏
        private String cjsj;      // 采集时间 yyyy-MM-dd HH:mm:ss
        private Double wd;        // 温度，保留一位小数
        private Double sd;        // 湿度，保留一位小数
    }
    
    /**
     * 验证结果内部类
     */
    @Data
    public static class ValidationResult {
        private int code;
        private String message;
        
        public ValidationResult(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
    
    /**
     * 验证请求参数
     * @return 验证结果，如果为null表示验证通过
     */
    public ValidationResult validate() {
        if (uscc == null || uscc.isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "uscc不能为空");
        }
        
        if (itemList == null || itemList.isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "data不能为空");
        }
        
        if (itemList.size() > 500) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "单次上传数据条数不能超过500条");
        }
        
        for (int i = 0; i < itemList.size(); i++) {
            HumitureData item = itemList.get(i);
            if (item.getSbazqy() == null || item.getSbazqy().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的sbazqy不能为空");
            }
            
            // 验证设备安装区域是否合法
            if (!EntityHumitureInfoEnum.CW.getValue().equals(item.getSbazqy()) && !EntityHumitureInfoEnum.YL.getValue().equals(item.getSbazqy()) && !EntityHumitureInfoEnum.LC.getValue().equals(item.getSbazqy())) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的sbazqy不合法，应为1(常温)、2(阴凉)或3(冷藏)");
            }
            
            if (item.getCjsj() == null || item.getCjsj().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的cjsj不能为空");
            }
            
            if (item.getWd() == null) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的wd不能为空");
            }
            
            // 验证温度范围是否合理（-50到100摄氏度）
            if (item.getWd() < -50.0 || item.getWd() > 100.0) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的wd超出合理范围(-50.0~100.0)");
            }
            
            if (!EntityHumitureInfoEnum.LC.getValue().equals(item.getSbazqy()) && item.getSd() == null) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的sd不能为空");
            }
            
            // 验证湿度范围是否合理（0到100%）
            if (item.getSd() != null && (item.getSd() < 0.0 || item.getSd() > 100.0)) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + (i + 1) + "条数据的sd超出合理范围(0.0~100.0)");
            }
        }
        
        return null;
    }
}